const maxDownloadsInput = document.querySelector("#max-downloads");

function clearDownload () {
  const fileItem1 = document.getElementById("status1");
  const fileItem2 = document.getElementById("status2");
  const fileItem3 = document.getElementById("status3");
  const fileItem4 = document.getElementById("status4");
  const fileItem5 = document.getElementById("status5");
  fileItem1.textContent = "";
  fileItem2.textContent = "";
  fileItem3.textContent = "";
  fileItem4.textContent = "";
  fileItem5.textContent = "";
};

function startDownload() {
  // 定义文件下载任务数组
  const downloadTasks = [
    () =>
      new Promise((resolve, reject) => {
        const fileItem = document.getElementById("status1");
        fileItem.textContent = "下载中...";
        // 模拟文件下载成功
        setTimeout(() => {
          fileItem.textContent = "下载成功";
          fileItem.classList.add("success");
          resolve("下载成功");
        }, 2000);
      }),
    () =>
      new Promise((resolve, reject) => {
        const fileItem = document.getElementById("status2");
        fileItem.textContent = "下载中...";
        // 模拟文件下载失败
        setTimeout(() => {
          fileItem.textContent = "下载失败";
          fileItem.classList.add("error");
          reject("下载失败");
        }, 3000);
      }),
    () =>
      new Promise((resolve, reject) => {
        const fileItem = document.getElementById("status3");
        fileItem.textContent = "下载中...";
        // 模拟文件下载成功
        setTimeout(() => {
          fileItem.textContent = "下载失败";
          fileItem.classList.add("error");
          reject("下载失败");
        }, 1000);
      }),
    () =>
      new Promise((resolve, reject) => {
        const fileItem = document.getElementById("status4");
        fileItem.textContent = "下载中...";
        // 模拟文件下载成功
        setTimeout(() => {
          fileItem.textContent = "下载成功";
          fileItem.classList.add("success");
          resolve("下载成功");
        }, 1000);
      }),
    () =>
      new Promise((resolve, reject) => {
        const fileItem = document.getElementById("status5");
        fileItem.textContent = "下载中...";
        // 模拟文件下载成功
        setTimeout(() => {
          fileItem.textContent = "下载成功";
          fileItem.classList.add("status", "success");
          resolve("下载成功");
        }, 1000);
      }),
    // 添加更多的文件下载任务...
  ];
  const limit = parseInt(maxDownloadsInput.value);

  // 控制并发下载数量为2
  concurrencyLimit(downloadTasks, limit)
    .then((results) => {
      console.log("所有文件下载完成");
      console.log(results);
    })
    .catch((error) => {
      console.log("文件下载出错");
      console.error(error);
    });
}

